#! /usr/bin/env/python3
# -*- coding:UTF-8 -*-
'''
    名称：AQI计算
    作者：王楠
    版本：1.0
    日期：2020/03/02
    更新：1：计算AQI
'''

IAQI = tuple(range(0, 501, 50))  # 空气质量分指数


def cal_liner(iqai_lo, iaqi_hi, bp_lo, bp_hi, cp):
    '''
    范围缩放
    '''
    iaqi = ((iaqi_hi-iqai_lo)*(cp - bp_lo))/(bp_hi - bp_lo) + iqai_lo
    return iaqi


def cal_pm_iaqi(pm_val):
    '''
    计算PM2.5的iaqi
    '''
    BP_PM = (0, 35, 75, 115, 150, 250, 350, 500)
    for i in range(len(BP_PM)-1):
        if BP_PM[i] <= pm_val <= BP_PM[i+1]:
            iaqi = cal_liner(BP_PM[i], BP_PM[i+1], IAQI[i], IAQI[i+1], pm_val)
    return iaqi


def cal_co_1h_iaqi(co_1h_val):
    '''
    计算co的iaqi
    '''
    BP_CO_1H = (0, 5, 10, 35, 60, 90, 120, 150)
    for i in range(len(BP_CO_1H)-1):
        if BP_CO_1H[i] <= co_1h_val <= BP_CO_1H[i+1]:
            iaqi = cal_liner(BP_CO_1H[i], BP_CO_1H[i+1], IAQI[i], IAQI[i+1], co_1h_val)
    return iaqi


def cal_aqi(param_list):
    '''
    函数：计算aqi并返回
    '''
    pm_val = param_list[0]
    co_1h_val = param_list[1]
    pm_iaqi = cal_pm_iaqi(pm_val)
    co_iaqi = cal_co_1h_iaqi(co_1h_val)
    print('pm_iaqi:', pm_iaqi)
    print('co_iaqi:', co_iaqi)
    iaqi_list = []
    iaqi_list.append(pm_iaqi)
    iaqi_list.append(co_iaqi)
    aqi = max(iaqi_list)

    return aqi


def aqi_main():
    '''
    主函数
    '''
    print('请输入一下信息(用1个空格隔开):')
    input_str = input('1、PM2.5,2、co：')
    str_list = input_str.split(' ')
    pm_val = float(str_list[0])
    co_1h_val = float(str_list[1])
    param_list = []
    param_list.append(pm_val)
    param_list.append(co_1h_val)
    # 调用计算函数
    aqi_val = cal_aqi(param_list)
    print('空气质量指数是{}'.format(aqi_val))


if __name__ == '__main__':
    aqi_main()
